<?php

namespace Common\Service;


/** 
 * 标签集合
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

Class TagService extends BaseService
{
	/**
	 title
	 */
	// 状态
	CONST STATUS_PEDDING = 0;	// 审核 pedding
	CONST STATUS_PASS 	 = 1;	// 展示 pass
	CONST STATUS_DEL  	 = -1;  // 删除 delete

	//ERROR 错误
	const ERROR_TITLE 		 = '标签不能为空';
	const ERROR_TITLE_UNIQUE = '标签已经存在';

	protected $_validate = array(
		array('title', 'require', self::ERROR_TITLE, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('title', '', self::ERROR_TITLE_UNIQUE, self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
	);

	protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', self::STATUS_PASS, self::MODEL_INSERT),
        array('search', 'getSearch', self::MODEL_INSERT, 'callback'),
        array('status', 'intval', self::MODEL_BOTH, 'function'),
        array('id', 'intval', self::MODEL_BOTH, 'function'),
        array('hot', 0, self::MODEL_INSERT),
        array('hot', 'intval', self::MODEL_BOTH, 'function'),
    );


	/** 
	 * 检查并返回正确的Tag
	 * @author Legend. <xcx_legender@qq.com>
	 * @param array $tags 
	 * @return array $right_tags
	 */
	
    public function checkTag($tags)
    {
    	$data = array();
    	$tags = array_unique($tags);
    	$map['title']  = array('in',  $tags);
    	$map['status'] = array('egt', self::STATUS_PEDDING);
    	$res = $this->where($map)->field('title')->select();
    	if ($res)
    	{
    		foreach ($res as $key => $value) {
    			$data[] = $value['title'];
    		}
    	}
    	return $data;
    }

    public function getSearch()
    {
        return explode(',', I('search', '' , 'trim'));
    }


    /** 
     * 增加热度
     * @author Legend. <xcx_legender@qq.com>
     * @param 
     * @return 
     */
    
    public function incrHot($tags, $value = 1)
    {
    	$map['title'] = array('in', $tags);
    	$this->where($map)->setInc('hot', $value);
    }


    /** 
     * 设置Hot
     * @author Legend. <xcx_legender@qq.com>
     * @param 
     * @return 
     */
    
    public function setHot($tag, $value)
    {
        $map['title'] = $tag;
        $this->where($map)->setField('hot', $value);
    }
	


}